<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="部门" prop="deptId">
        <el-cascader :props="{ value: 'id', checkStrictly: true }" placeholder="请选择部门" v-model="queryParams.deptId"
          :options="deptOptions"></el-cascader>
      </el-form-item>
      <el-form-item label="日期">
        <el-date-picker v-model="dateRange" value-format="yyyy-MM-dd HH:mm:ss" type="daterange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期" style="width: 240px"
          :default-time="['00:00:00', '23:59:59']"></el-date-picker>
      </el-form-item>
      <el-form-item label="合同编号">
        <el-input
          v-model="queryParams.htbh"
          placeholder="请输入合同编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['equipment:contractReview:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['equipment:contractReview:edit']">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['equipment:contractReview:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="printBox"
          v-hasPermi="['equipment:contractReview:print']">打印</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table @selection-change="handleSelectionChange" border fit v-loading="loading" :data="dataList">
      <el-table-column type="selection" width="55" :selectable="selectable" :disabled="true" align="center" />
      <el-table-column label="序号" type="index" />
      <!-- <el-table-column label="资格证书" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ handleString(scope.row.contentList) || "--" }}</span>
        </template>
      </el-table-column> -->
      <el-table-column align="center" label="填表人" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-link @click="handleDetail(scope.row)" type="primary">{{
            scope.row.userName
          }}</el-link>
        </template>
      </el-table-column>

      <el-table-column align="center" show-overflow-tooltip label="填表部门" prop="deptName">
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip label="填表日期" prop="fillTime">
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip label="检测项目" prop="jcxm">
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip label="委托单位" prop="wtdw">
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip label="合同编号" prop="htbh">
      </el-table-column>
      <el-table-column show-overflow-tooltip label="流程状态" width="100">
        <template slot-scope="scope">
          <el-tag size="mini">{{ scope.row.status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="240" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" v-hasPermi="['equipment:contractReview:info']"
            @click="handleDetail(scope.row)">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['equipment:contractReview:edit']">修改</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['equipment:applyWork:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <!-- 打印模板位置 -->
    <PrintBox :printRow="printList" id="printBox">
      <template slot="title" slot-scope="scope">
        <div style="margin-bottom: 20px">
          新开展项目/重大项目/仲裁项目合同评审记录表 STET/B/04-1
        </div>
      </template>
      <template slot-scope="scope">
        <!-- 表格  class  table-page  必须-->
        <table style="border-collapse: collapse; width: 100%" border="1" cellspacing="0" class="table-page"
          :isVertical="false">
          <tr style="height: 5mm">
            <td colspan="1" width="25%">填表部门</td>
            <td colspan="3" width="25%">{{ scope.item.deptName }}</td>
            <td colspan="1" width="25%">填表人/日期</td>
            <td colspan="3" width="25%">{{ scope.item.userName }}/{{ scope.item.fillTime }}</td>
          </tr>
          <tr style="height: 5mm">
            <td colspan="1">检测项目</td>
            <td colspan="6">{{ scope.item.jcxm }}</td>
          </tr>
          <tr style="height: 5mm">
            <td colspan="1" width="25%">委托单位</td>
            <td colspan="2" width="25%">{{ scope.item.wtdw }}</td>
            <td colspan="2">合同编号</td>
            <td colspan="2">{{ scope.item.htbh }}</td>
          </tr>
          <tr style="height: 5mm">
            <td colspan="2" width="25%">评审要素</td>
            <td colspan="3" width="25%">评审记录</td>
            <td colspan="3" width="25%">备注</td>
          </tr>
          <tr style="height: 5mm">
            <td colspan="1" width="10%" rowspan="3">人员配备</td>
            <td colspan="2" width="25%">项目负责人</td>
            <td colspan="2" width="25%">{{ scope.item.xmfzrps }}</td>
            <td colspan="2" width="25%">{{ scope.item.xmfzrbz }}</td>
          </tr>
          <tr style="height: 5mm">
            <td colspan="2" width="25%">技术负责人</td>
            <td colspan="2" width="25%">{{ scope.item.jsfzrps }}</td>
            <td colspan="2" width="25%">{{ scope.item.jsfzrbz }}</td>

          </tr>
          <tr style="height: 5mm">
            <td colspan="2" width="25%">其他主要检测人员</td>
            <td colspan="2" width="25%">{{ scope.item.qtrps }}</td>
            <td colspan="2" width="25%">{{ scope.item.qtrbz }}</td>
          </tr>
          <tr style="height: 5mm">
            <td colspan="3" width="25%">设备配置</td>
            <td colspan="2" width="25%">{{ scope.item.zbpzps }}</td>
            <td colspan="3" width="25%">{{ scope.item.zbpzbz }}</td>
          </tr>
          <tr style="height: 5mm">
            <td colspan="3" width="25%">器具耗材</td>
            <td colspan="2" width="25%">{{ scope.item.qjhcps }}</td>
            <td colspan="3" width="25%">{{ scope.item.qjhcbz }}</td>
          </tr>
          <tr style="height: 5mm">
            <td colspan="3" width="25%">检测方法标准</td>
            <td colspan="2" width="25%">{{ scope.item.jcffbzps }}</td>
            <td colspan="3" width="25%">{{ scope.item.jcffbzbz }}</td>
          </tr>
          <tr style="height: 5mm">
            <td colspan="3" width="25%">环境条件</td>
            <td colspan="2" width="25%">{{ scope.item.hjtjps }}</td>
            <td colspan="3" width="25%">{{ scope.item.hjtjbz }}</td>
          </tr>
          <tr style="height: 5mm">
            <td colspan="3" width="25%">委托方特殊要求</td>
            <td colspan="2" width="25%">{{ scope.item.wtftsyqps }}</td>
            <td colspan="3" width="25%">{{ scope.item.wtftsyqbz }}</td>
          </tr>
          <tr style="height: 5mm">
            <td colspan="3" width="25%">进度</td>
            <td colspan="2" width="25%">{{ scope.item.jdps }}</td>
            <td colspan="3" width="25%">{{ scope.item.jdbz }}</td>
          </tr>
          <tr style="height: 5mm">
            <td colspan="3" width="25%">检测费标准及支付方式</td>
            <td colspan="2" width="25%">{{ scope.item.jcfbzps }}</td>
            <td colspan="3" width="25%">{{ scope.item.jcfbzbz }}</td>
          </tr>
          <tr style="height: 5mm">
            <td colspan="3" width="25%">合法合规情况</td>
            <td colspan="2" width="25%">{{ scope.item.hfhgqkps }}</td>
            <td colspan="3" width="25%">{{ scope.item.hehgqkbz }}</td>
          </tr>
          <tr style="height: 5mm">
            <td colspan="3" width="25%">分包情况</td>
            <td colspan="2" width="25%">{{ scope.item.fbqkps?scope.item.fbqkps:'不分包' }}</td>
            <td colspan="3" width="25%">{{ scope.item.fbqkbz }}</td>
          </tr>
          <tr style="height: 5mm">
            <td colspan="1" width="25%">技术质量部门意见</td>
            <td colspan="7" width="25%">
              <div style="display: flex; align-items: center">
                <div style="display: flex; align-items: center">
                </div>
                <div style="display: flex; align-items: center">
                  <div style="
                      display: flex;
                      align-items: center;
                      width: 55mm;
                      margin-left: 10mm;
                    ">
                    <input type="checkbox" :checked="checkHistoryNodeComment(scope, 'jszlbm').type == 1
                      " />满意资源配置、技术准备要求
                  </div>
                  <div style="
                      display: flex;
                      align-items: center;
                      width: 55mm;
                      margin-left: 10mm;
                    ">
                    <input type="checkbox" :checked="checkHistoryNodeComment(scope, 'jszlbm').type == 3
                      " />不满意资源配置、技术准备要求
                  </div>
                </div>
              </div>
              <div style="
                  display: flex;
                  justify-content: flex-end;
                  text-align: left;
                  margin-top: 10px;
                ">
                <div style="width: 58mm;display: flex;align-items: center;">部门负责人：
                  <template v-if="CheckApprove(scope, 'jszlbm').ShowImg">
                    <img style="width: 80px; " :src="CheckApprove(scope, 'jszlbm').ApproveImg" />
                  </template>
                </div>
                <div style="width: 40mm">日期：{{ checkHistoryNode(scope, "jszlbm").endTime }}</div>
              </div>

            </td>
          </tr>
          <tr style="height: 5mm">
            <td colspan="1" width="25%">质量负责人意见</td>
            <td colspan="7" width="25%">
              <div style="
                position: absolute;
                top: 30px;
                left: 10px;
                text-align: left;
                
              ">
              </div>
              <div style="display: flex; align-items: center">
                <div style="display: flex; align-items: center; width: 30mm;margin-left: 10mm;">
                  <input type="checkbox" :checked="checkHistoryNodeComment(scope, 'zlfzr').type == 1" />同意
                </div>
                <div style="
                  display: flex;
                  align-items: center;
                  width: 30mm;
                  margin-left: 10mm;
                ">
                  <input type="checkbox" :checked="checkHistoryNodeComment(scope, 'zlfzr').type == 2 ||
                    checkHistoryNodeComment(scope, 'zlfzr').type == 3
                    " />不同意
                </div>
              </div>
              <div style="
                display: flex;
                justify-content: flex-end;
                text-align: left;
                margin-top: 30px;
              ">
                <div style="width: 40mm; position: relative">
                  质量负责人：<template v-if="CheckApprove(scope, 'zlfzr').ShowImg">
                    <img style="width: 80px; position: absolute; top: -10px"
                      :src="CheckApprove(scope, 'zlfzr').ApproveImg" />
                  </template>
                </div>
                <div style="width: 40mm">
                  日期：{{ checkHistoryNode(scope, "zlfzr").endTime }}
                </div>
              </div>

            </td>
          </tr>
          <tr style="height: 5mm">
            <td colspan="1" width="25%">技术负责人意见</td>
            <td colspan="7" width="25%">
              <div style="
                position: absolute;
                top: 30px;
                left: 10px;
                text-align: left;
                
              ">
              </div>
              <div style="display: flex; align-items: center">
                <div style="display: flex; align-items: center; width: 30mm;margin-left: 10mm;">
                  <input type="checkbox" :checked="checkHistoryNodeComment(scope, 'zlfzr').type == 1" />同意
                </div>
                <div style="
                  display: flex;
                  align-items: center;
                  width: 30mm;
                  margin-left: 10mm;
                ">
                  <input type="checkbox" :checked="checkHistoryNodeComment(scope, 'zlfzr').type == 2 ||
                    checkHistoryNodeComment(scope, 'zlfzr').type == 3
                    " />不同意
                </div>
              </div>
              <div style="
                display: flex;
                justify-content: flex-end;
                text-align: left;
                margin-top: 30px;
              ">
                <div style="width: 40mm; position: relative">
                  技术负责人：<template v-if="CheckApprove(scope, 'jsfzr').ShowImg">
                    <img style="width: 80px; position: absolute; top: -10px"
                      :src="CheckApprove(scope, 'jsfzr').ApproveImg" />
                  </template>
                </div>
                <div style="width: 40mm">
                  日期：{{ checkHistoryNode(scope, "jsfzr").endTime }}
                </div>
              </div>
            </td>
          </tr>
          <tr style="height: 5mm">
            <td colspan="1" width="25%">审批意见</td>
            <td colspan="7" width="25%">
              <div style="
                position: absolute;
                top: 30px;
                left: 10px;
                text-align: left;
                
              ">
              </div>
              <div style="display: flex; align-items: center">
                <div style="display: flex; align-items: center; width: 60mm;margin-left: 10mm;">
                  <input type="checkbox" :checked="checkHistoryNodeComment(scope, 'zjl').type == 1" />同意新开展项目/重大项目/仲裁项目
                </div>
                <div style="
                  display: flex;
                  align-items: center;
                  width: 60mm;
                  margin-left: 10mm;
                ">
                  <input type="checkbox" :checked="checkHistoryNodeComment(scope, 'zjl').type == 2 ||
                    checkHistoryNodeComment(scope, 'zjl').type == 3
                    " />不同意新开展项目/重大项目/仲裁项目
                </div>
              </div>
              <div style="
                display: flex;
                justify-content: flex-end;
                text-align: left;
                margin-top: 30px;
              ">
                <div style="width: 40mm; position: relative">
                  总经理：<template v-if="CheckApprove(scope, 'zjl').ShowImg">
                    <img style="width: 80px; position: absolute; top: -10px"
                      :src="CheckApprove(scope, 'zjl').ApproveImg" />
                  </template>
                </div>
                <div style="width: 40mm">
                  日期：{{ checkHistoryNode(scope, "zjl").endTime }}
                </div>
              </div>

            </td>
          </tr>
        </table>
        <!-- 底部信息位置 -->
        <div class="page-bottom" style="page-break-after: always">
          <div class="desc">注：本表格自2022年4月20日起生效</div>
        </div>
      </template>
    </PrintBox>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import { projectReviewRecordList, projectReviewRecordDel } from "@/api/business/contractReview.js";
import { deptTreeSelect } from "@/api/system/user.js";
import printStyle from "@/utils/print.js";

export default {
  name: "applyWork",
  data() {
    return {
      baseUrl: this.$VUE_APP_IMG_API,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      dataList: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceName: undefined,
        operUserId: undefined,
        licenseNum: undefined,
        checkDate: null,
        deptId: [],
      },
      // 日期范围
      dateRange: [],
      checkboxGroupSHYJ: [],
      userList: [],
      deptOptions: [],
      infos: [], //勾选的数据
      printList: [],
      checkboxGroupJSZL: [],
      checkboxGroupJCBM: [],
      SHYJTime: '',
      JSZLTime: '',
      JCBMTime: '',
      KHCJSMTime: ''
    };
  },
  created() {
    const str = sessionStorage.getItem('queryParams');
    if (str) {
      this.queryParams = JSON.parse(str);
      if (this.queryParams.startEndDate) {
        this.dateRange.push(this.queryParams.startEndDate, this.queryParams.endEndDate)
      }
      setTimeout(() => {
        sessionStorage.removeItem('queryParams');
      }, 500)
    }
    this.getList();
    this.getDeptTree();
  },
  methods: {
    selectable(row, index) {
      if (row.statusName == '审核中') {
        return false         //禁用
      } else {
        return true          //可选
      }
    },
    printBox() {
      //infos  勾选的数据 如果有勾选就打印勾选。
      console.log(this.infos.length);
      if (this.infos.length) {
        this.printList = this.infos;
      } else {
        //反之打印当前整个列表
        this.printList = this.dataList;
      }
      setTimeout(() => {
        document.title = "仪器设备调整校准周期记录表 STET/B/22-5";
        this.$print({
          printable: "printBox", //打印区域id
          type: "html", //打印类型是html
          scanStyles: false,
          targetStyles: ["*"],
          style: this.workStyle(),
          onPrintDialogClose: () => {
            document.title = process.env.VUE_APP_TITLE;
          },
        });
      }, 500);
    },

    // 可以自定义样式  printStyle 是公共样式 切勿私自修改
    workStyle() {
      return `${printStyle}
        table,
        table tr th,
        table tr td {
          padding: 4px;
          height:35px
        }
        .desc{
        text-align: left; font-size: 12px; margin-top: 10px
      }
            .df {
                display: flex;
              }
      `;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.projectReviewRecordId);
      this.infos = selection;
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));
      const configId = row.projectReviewRecordId || this.ids;
      this.$router.push("./addContractReview?isEdit=true&id=" + configId);
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    handleDetail(row) {
      // sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));
      // this.$router.push("./addContractReview?isInfo=true&id=" + row.projectReviewRecordId);
      if (row.status == 'draft') {
        // this.$router.push("./addBuy?isInfo=true&id=" + row.deviceBuyId);
      } else {
        sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));
        this.$router.push({
          path: "/buy/process/detail/" + row.piid,
          query: {
            definitionId: null,
            deployId: null,
            taskId: null,
            finished: false,
            isInfo: true,
            id: row.projectReviewRecordId,
            value: 1
          },
        });
      }
    },
    handleString(e) {
      return e.map((i) => i.certTypeName).join(",");
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const configId = row.projectOtherContractId || this.ids;
      let name = this.checkRow(
        configId,
        this.dataList,
        "projectReviewRecordId",
        "DELETE",
        "userName"
      );
      this.$modal
        .confirm('是否确认删除员工为"' + name + '"的数据项？')
        .then(function () {
          return projectReviewRecordDel(configId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    handleAdd() {
      this.$router.push("./addContractReview");
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      projectReviewRecordList({
        ...this.addDateRangeTime(
          this.queryParams,
          this.dateRange,
          "startEndDate",
          "endEndDate"
        ),
        deptId: this.queryParams.deptId.slice(-1)[0],
      }).then((response) => {
        this.dataList = response.rows;
        // 审批意见

        this.dataList.forEach((item, index) => {
          if (item.statusName == '已完成') {

            let SHYJ = item.wfDetailVo.historyProcNodeList[1].commentList[0].fullMessage.split(']')[0].slice(2)
            SHYJ = SHYJ.substr(0, SHYJ.length - 1)
            item.checkboxGroupSHYJ = []
            item.checkboxGroupSHYJ.push(SHYJ)
            item.SHYJTime = item.wfDetailVo.historyProcNodeList[1].commentList[0].time.substr(0, 10)
            let JSZL = item.wfDetailVo.historyProcNodeList[2].commentList[0].fullMessage.split(']')[0].slice(2)
            let reg = new RegExp('"')
            JSZL = JSZL.replace(reg, '').replace(reg, '').replace(reg, '')
            JSZL = JSZL.split(',')
            console.log(JSZL);
            item.checkboxGroupJSZL = []
            item.checkboxGroupJSZL = JSZL
            item.JSZLTime = item.wfDetailVo.historyProcNodeList[2].commentList[0].time.substr(0, 10)

            let JCBM = item.wfDetailVo.historyProcNodeList[3].commentList[0].fullMessage.split(']')[0].slice(2)
            JCBM = JCBM.replace(reg, '').replace(reg, '').replace(reg, '')
            JCBM = JCBM.split(',')
            item.checkboxGroupJCBM = []
            item.checkboxGroupJCBM = JCBM
            item.JCBMTime = item.wfDetailVo.historyProcNodeList[3].commentList[0].time.substr(0, 10)

            item.KHCJSM = item.wfDetailVo.historyProcNodeList[4].commentList[0].message.split('@')[0]
            item.KHCJSMTime = item.wfDetailVo.historyProcNodeList[4].commentList[0].time.substr(0, 10)
          }

        })
        // let SHYJ = this.dataList[0].wfDetailVo.historyProcNodeList[1].commentList[0].fullMessage.split(']')[0].slice(2)      
        // SHYJ = SHYJ.substr(0, SHYJ.length - 1)
        // 技术质量部门审核以及按
        // let JSZL = this.dataList[0].wfDetailVo.historyProcNodeList[1].commentList[0].fullMessage.split(']')[0].slice(2)      
        // JSZL = JSZL.substr(0, SHYJ.length - 1)
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleExport() {
      this.queryParams.ids = this.ids.toString()
      this.download(
        "system/projectReviewRecord/export",
        {
          ...this.queryParams,
        },
        `合同评审${this.parseTime(
          new Date().getTime(),
          "{y}-{m}-{d}"
        )}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__body {
  img {
    width: 100%;
  }
}

.table-page {
  width: 100%;
}
</style>
